package com.cuicuisha.gxyp.user.controller;

import com.cuicuisha.gxyp.model.dto.user.UserLoginDto;
import com.cuicuisha.gxyp.model.dto.user.UserRegisterDto;
import com.cuicuisha.gxyp.model.entity.system.SysUser;
import com.cuicuisha.gxyp.model.entity.system.SysUserAuthContextUtil;
import com.cuicuisha.gxyp.model.entity.user.AuthContextUtil;
import com.cuicuisha.gxyp.model.entity.user.UserInfo;
import com.cuicuisha.gxyp.model.vo.common.Result;
import com.cuicuisha.gxyp.model.vo.common.ResultCodeEnum;
import com.cuicuisha.gxyp.model.vo.user.UserInfoVo;
import com.cuicuisha.gxyp.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "会员用户接口")
@RestController
@RequestMapping("/api/user/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "会员注册")
    @PostMapping("register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {
        userInfoService.register(userRegisterDto);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "会员登录")
    @PostMapping("login")
    public Result login(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        return Result.build(userInfoService.login(userLoginDto, ip), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
        UserInfo userInfo = AuthContextUtil.getUser();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setAvatar(userInfo.getAvatar());
        userInfoVo.setNickName(userInfo.getNickName());
        return Result.build(userInfoVo , ResultCodeEnum.SUCCESS) ;
    }

    // 网关微服务鉴权调用,微服务内部间调用接口
    @GetMapping("/auth/checkUserInfo/{token}")
    public UserInfo checkUserInfo(HttpServletRequest request,@PathVariable String token) {
//        String token = request.getHeader("token");
        UserInfo userInfo = userInfoService.checkUserInfo(token);
        return userInfo;
    }

}